<?php
// @formatter:off
/**
 * @file ct.statistics.update.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: ct.statistics.update.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/cron/ct.statistics.update.php $
 */
// @formatter:on

class WCUpdateStatistics extends WCCronTool {
	public function run($spacer = "") {
		global $wcOptions;

		//
		// Updating tags' counts.
		if($wcOptions->activated("Tags")) {
			$this->updateTags($spacer);
		}

		//
		// Updating directories' counts.
		if($wcOptions->activated("Directories")) {
			$this->updateDirectories($spacer);
		}

		//
		// Updating comic ratings.
		if($wcOptions->activated("ReadRates")) {
			$this->updateReadRates($spacer);
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}This update possible misscalculated counts and statistics.\n\n";
		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-d, --directories\n";
		echo "{$spacer}\t\tUpdate directories' count of comics.\n\n";
		echo "{$spacer}\t-t, --tags\n";
		echo "{$spacer}\t\tUpdate tags' count of members.\n\n";
		echo "{$spacer}\t-r, --rates\n";
		echo "{$spacer}\t\tUpdate comics' reads/rates counts.\n\n";
	}
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;

			if($wcOptions->addOption("Tags")) {
				$wcOptions->addOptionTrigger("Tags", "t");
				$wcOptions->addOptionTrigger("Tags", "tags");
			}
			if($wcOptions->addOption("Directories")) {
				$wcOptions->addOptionTrigger("Directories", "d");
				$wcOptions->addOptionTrigger("Directories", "directories");
			}
			if($wcOptions->addOption("ReadRates")) {
				$wcOptions->addOptionTrigger("ReadRates", "r");
				$wcOptions->addOptionTrigger("ReadRates", "rates");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
	protected function updateDirectories($spacer = "") {
		//
		// Loading global pointers.
		global $wcDefaults;
		//
		// Checking that Read Only Mode is not activated.
		if(!$wcDefaults["read-only"]) {
			$stmt = null;
			$stmtId = __CLASS__."::".__FUNCTION__;
			if($this->_db->has($stmtId)) {
				$stmt = $this->_db->get($stmtId);
			} else {
				$query = "update  {$this->_dbprefix}directories\n";
				$query .= "set     dir_count = (\n";
				$query .= "                select  count(*)\n";
				$query .= "                from    {$this->_dbprefix}comics\n";
				$query .= "                where   cmc_directory = dir_id\n";
				$query .= "        )";

				$stmt = $this->_db->prepare($stmtId, $query);
			}

			echo "{$spacer}Updating directories' counts: ";
			$stmt->execute();
			echo $stmt->rowCount()." directories updated\n";
		} else {
			echo "{$spacer}".WCColorText::ShellYellow("ReadOnlyMode activated.\n");
		}
	}
	protected function updateReadRates($spacer = "") {
		global $wcReadRate;

		echo "{$spacer}Updating comic reads/rates: ";
		echo $wcReadRate->updateStats();
		echo " comics updated\n";
	}
	protected function updateTags($spacer = "") {
		//
		// Loading global pointers.
		global $wcDefaults;
		//
		// Checking that Read Only Mode is not activated.
		if(!$wcDefaults["read-only"]) {
			$stmt = null;
			$stmtId = __CLASS__."::".__FUNCTION__;
			if($this->_db->has($stmtId)) {
				$stmt = $this->_db->get($stmtId);
			} else {
				$query = "update  {$this->_dbprefix}tags\n";
				$query .= "set     tag_count = (\n";
				$query .= "                select  count(*)\n";
				$query .= "                from    {$this->_dbprefix}tag_members\n";
				$query .= "                where   mem_tag = tag_id\n";
				$query .= "        )";

				$stmt = $this->_db->prepare($stmtId, $query);
			}

			echo "{$spacer}Updating tags' counts: ";
			$stmt->execute();
			echo $stmt->rowCount()." tags updated\n";
		} else {
			echo "{$spacer}".WCColorText::ShellYellow("ReadOnlyMode activated.\n");
		}
	}
}
?>
